<?php 
	/*
		Copyright 2007, 2008 Raphael Iloh
	
		This file is part of Personal Appointment Manager (PAM).
	
		PAM is a free software: you can redistribute it and/or modify
		it under the terms of the GNU General Public License as published by
		the Free Software Foundation, either version 3 of the License, or
		(at your option) any later version.
	
		PAM is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
		GNU General Public License for more details.
	
		You should have received a copy of the GNU General Public License
		along with PAM.  If not, see <http://www.gnu.org/licenses/>.
	*/
	
	/*
		This script is used by administrator to manage registered user accounts
		Only administrators have access to this page
	*/
	
	define('IS_VALID_PAM', true); // ensure application is accessed correctly
	$page_title = "Create New Diary";
	$content_title = "Create New Diary";
	include_once('./Includes/header.php');
	if(file_exists(PAM_CONFIG)) { // don't run this page if config file already exist
		$url = getPageUrl($_SERVER['PHP_SELF']) . "/index.php";
		header("Location: $url");
		exit;
	}

	if (isset($_POST['submitted'])) { // Check if the form has been submitted.
		if($_POST['submitted'] == 'FALSE'){
			$displayedMessage = "Please complete the registration form, all fields are required.";
		}
		else{
			$fn; $ln; $e; $p; $dso; $conf;
			if(validateUserData($displayedMessage, $_POST, $fn, $ln, $e, $p, $dso)){ // If everything's OK.
				$retval = FALSE;
				switch($dso){
					case 'radio_ws':
						$conf = 'Your diary has been created successfully<br /><br />' .
						'To validate your email address, an email has been sent to your email address: $e<br /><br />' .
						'Please click on the link provided in the email to access your new diary';
						$retval = isValidWSDL($displayedMessage, $_POST); break;
					case 'radio_db':
						$conf = 'Your diary has been created successfully<br /><br />' .
						'Please <a href="./login.php" title="click here to access your new diary">click here</a> to login';
						$retval = isValidDBServer($displayedMessage, $_POST, $fn, $ln, $e, $p, $dso); break;
				}
				if($retval){
					$_SESSION['confirmation_message'] = htmlentities($conf);
					$url = getPageUrl($_SERVER['PHP_SELF']) . '/confirmation.php';
					header("Location: $url");
					//exit(); // Quit the script.
					//$displayedMessage = "Diary setup has completed successfully";
				}
				else{
					$displayedMessage .= "<br /><br />Diary setup has failed!";
				}
			} else { // at least one of the data supplied failed validation.
				$displayedMessage .= "Please try again<br />";
			}
		}
	}
  //echo getFileContents('./Templates/MeetingInvite.txt');
  if(!empty($displayedMessage)){
  	echo prepareMsg($displayedMessage);
		$displayedMessage = ""; // reset after displaying message
  }
	
	function isValidWSDL(&$msg, $post){
		$retval = FALSE;
		
		return $retval;
	}

	function isValidDBServer(&$msg, $post, $fn, $ln, $e, $p, $dso){
		require_once('./Includes/dbfactory.php');
		require_once('./Includes/mailfactory.php');
		$retval = FALSE;
		$srv; $db; $uid; $pwd; //$db_exists;
		$s_srv; $s_uid; $s_pwd;
		$retval = (validateDBSettings($msg, $post, $srv, $db, $uid, $pwd) && validateSmtpSettings($msg, $post, $s_srv, $s_uid, $s_pwd));
		if($retval){
			$msg = ""; //clear message
			$retval = (createDB($msg, $srv, $db, $uid, $pwd) && sendTestMail($msg, $s_srv, $s_uid, $s_pwd, $fn, $ln, $e));
			if($retval){
				$diaryToken = GenerateGUID();
				$userToken = GenerateGUID();
				$retval = createDiary($msg, $srv, $db, $uid, $pwd, $fn, $ln, $e, $p, $dso, $diaryToken, $userToken);
				if($retval){
					createConfigFile($diaryToken, $dso, $srv, $db, $uid, $pwd, $s_srv, $s_uid, $s_pwd, 'FALSE');
					$retval = sendConfirmationMail($msg, $fn, $ln, $e, $diaryToken);
				}
			}
		}
		
		return $retval;
	}
?>
	<div style="visibility: hidden; display: none; padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px;" id="msgDivCont">
    <br /><div class="msgDiv" id="msgDiv"></div><br />
    </div>
    <p style="font-weight: bold">To create a new diary, please provide all information. 
Password may contain between 4 and 8 alphanumeric characters.</p>
<div style="width: 70%; text-align: left;">
	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
		<fieldset>
			<legend>Diary Owner Information</legend>
		<table style="width: 100%; border-width: 0px; text-align: center;"border="0" cellpadding="2" cellspacing="2">
			<tr> 
				<td style="width: 35%; text-align: left; font-weight: bold;">First name</td>
				<td style="width: 65%; text-align: left;">
					<input type="text" name="first_name" id="first_name" class="textbox2" maxlength="15" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" />
				</td>
			</tr>
			<tr> 
				<td style="width: 35%; text-align: left; font-weight: bold;">Last name</td>
				<td style="width: 65%; text-align: left;">
					<input type="text" id="last_name" name="last_name"  class="textbox2" maxlength="30" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" />
				</td>
			</tr>
			<tr> 
				<td style="width: 35%; text-align: left; font-weight: bold;">Email Address</td>
				<td style="width: 65%; text-align: left;">
					<input type="text" id="email_add" name="email_add"  class="textbox2" maxlength="30" value="<?php if (isset($_POST['email_add'])) echo $_POST['email_add']; ?>" />
				</td>
			</tr>
			<tr> 
				<td style="width: 35%; text-align: left; font-weight: bold;">Password</td>
				<td style="width: 65%; text-align: left;">
					<input type="password" id="user_pwd" name="user_pwd"  class="textbox2" maxlength="30" value="" />
				</td>
			</tr>
			<tr> 
				<td style="width: 35%; text-align: left; font-weight: bold;">Confirm Password</td>
				<td style="width: 65%; text-align: left;">
					<input type="password" id="user_pwd2" name="user_pwd2"  class="textbox2" maxlength="30" value="" />
				</td>
			</tr>
		</table>
		</fieldset><br />
		<fieldset>
			<legend>Diary Settings</legend>
            <div class="stdDiv">
            <input name="data_src" type="radio" <?php echo ((isset($_POST['data_src']) && ($_POST['data_src'] == 'radio_ws')) ? 'checked' : ''); ?> onclick="SetVisibility(this, 'div_WS', 'div_DB');" value="radio_ws" id="radio_ws" /><label for="radio_ws">
            I want to use PAM web services</label></div>
            <div class="stdDiv" <?php echo ((isset($_POST['data_src']) && ($_POST['data_src'] == 'radio_ws')) ? 'style="visibility: visible;"' : 'style="visibility: hidden; display: none"'); ?>  id="div_WS">
            <br />
            <table style="width: 100%; border-width: 0px; text-align: left;" cellspacing="2" cellpadding="2">
  <tr>
    <td style="width: 35%; text-align: left; font-weight: bold;">Webservice WSDL Url</td>
    <td style="width: 60%; text-align: left;"><input id="ws_wsdl" name="ws_wsdl"  class="textbox" maxlength="120" value="http://www.mypam.com/pamServices.asmx?wsdl" /></td>
    <td style="width: 5%; text-align: left;">&nbsp;</td>
    </tr>
    </table>
            </div>
            <div class="stdDiv">
            <input name="data_src" type="radio" <?php echo ((isset($_POST['data_src']) && ($_POST['data_src'] == 'radio_db')) ? 'checked' : ''); ?> onclick="SetVisibility(this, 'div_DB', 'div_WS');" value="radio_db" id="radio_db" /><label for="radio_db">
            I have access to MySQL database &amp; I want to use it instead</label>
            </div>
            <div class="stdDiv" <?php echo ((isset($_POST['data_src']) && ($_POST['data_src'] == 'radio_db')) ? 'style="visibility: visible;"' : 'style="visibility: hidden; display: none"'); ?> id="div_DB">
            <br />
            <table style="width: 73%; border-width: 0px; text-align: left;" cellspacing="2" cellpadding="2">
  <tr>
    <td style="width: 47%; text-align: left; font-weight: bold;">MySQL Server Hostname</td>
    <td style="width: 53%; text-align: left;"><input id="mysql_srv" name="mysql_srv"  class="textbox2" maxlength="30" value="<?php if (isset($_POST['mysql_srv'])) echo $_POST['mysql_srv']; ?>" /></td>
  </tr>
  <tr>
    <td style="text-align: left; font-weight: bold;">MySQL database Name</td>
    <td><input id="mysql_db" name="mysql_db"  class="textbox2" maxlength="30" value="<?php if (isset($_POST['mysql_db'])) echo $_POST['mysql_db']; ?>" /></td>
  </tr>
  <tr>
    <td style="text-align: left; font-weight: bold;">MySQL User ID</td>
    <td><input id="mysql_userid" name="mysql_userid"  class="textbox2" maxlength="30" value="<?php if (isset($_POST['mysql_userid'])) echo $_POST['mysql_userid']; ?>" /></td>
  </tr>
  <tr>
    <td style="text-align: left; font-weight: bold;">MySQL User Password</td>
    <td><input type="password" id="msql_pwd" name="mysql_pwd"  class="textbox2" maxlength="30" value="" /></td>
  </tr>
  <tr>
    <td colspan="2" style="text-align: left; font-weight: bold;">&nbsp;</td>
  </tr>
  <tr>
    <td style="text-align: left; font-weight: bold;">SMTP Server Name/IP</td>
    <td><input id="smtp_srv" name="smtp_srv"  class="textbox2" maxlength="30" value="<?php if (isset($_POST['smtp_srv'])) echo $_POST['smtp_srv']; ?>" /></td>
  </tr>
  <tr>
    <td style="text-align: left; font-weight: bold;">SMTP Username</td>
    <td><input id="smtp_userid" name="smtp_userid"  class="textbox2" maxlength="30" value="<?php if (isset($_POST['smtp_userid'])) echo $_POST['smtp_userid']; ?>" /></td>
  </tr>
  <tr>
    <td style="text-align: left; font-weight: bold;">SMTP User Password</td>
    <td><input type="password" id="smtp_pwd" name="smtp_pwd"  class="textbox2" maxlength="30" value="" /></td>
  </tr>
</table>
            </div>
		</fieldset><br />
   	  <div class="stdDiv">
          <input type="submit" onclick="return Register('submitted', 'user_pwd', 'user_pwd2', 'email_add', 'last_name', 'first_name');" name="submit" value="Create New Diary" />
   	  </div>
        <input id="submitted" type="hidden" name="submitted" value="True" />
	</form>
</div>
<?php include_once('./Includes/footer.php'); ?>